﻿using System.Windows.Input;
using BHS.TFSChangsetAnalyser.Model;
using BHS.TFSChangsetAnalyser.ViewModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;

namespace BHS.TFSChangsetAnalyser.UnitTests
{
    [TestClass]
    public class AnalyseCommandTests
    {
        private ICommand _command;
        private IMainWindowViewModel _mockViewModel;
        private IErrorPresenter _mockErrorPresenter;


        [TestInitialize]
        public void TestInitialise()
        {
            _mockViewModel = MockRepository.GenerateMock<IMainWindowViewModel>();
            _mockErrorPresenter = MockRepository.GenerateMock<IErrorPresenter>();

            _command = new AnalyseCommand(_mockViewModel, _mockErrorPresenter);
        }

        [TestMethod]
        public void CanExecuteDisabledIfNotUsingDefaultCredentialsAndNoneSupplied()
        {
            // Arrange
            TfsServerConnectionDetails details = new TfsServerConnectionDetails
                                                     {
                                                         UseDefaultCredentials = false,
                                                         Username = string.Empty,
                                                         Password = string.Empty,
                                                         DomainName = string.Empty,
                                                         TfsServerUrl = "http://localhost/tfs",
                                                         TfsProjectPath = "$MyProject"
                                                     };

            _mockViewModel.Expect(x => x.TfsServerConnectionDetails).Return(details).Repeat.Any();

            // Act 
            bool canExecute = _command.CanExecute(null);

            // Assert
            Assert.IsFalse(canExecute, "Should not be able to execute command without using default credentials or passing new ones");
        }

        [TestMethod]
        public void CanExecuteEnabledIfUsingDefaultCredentials()
        {
            // Arrange
            TfsServerConnectionDetails details = new TfsServerConnectionDetails
                                                     {
                                                         UseDefaultCredentials = true,
                                                         Username = string.Empty,
                                                         Password = string.Empty,
                                                         DomainName = string.Empty,
                                                         TfsServerUrl = "http://localhost/tfs",
                                                         TfsProjectPath = "$MyProject"
                                                     };

            _mockViewModel.Expect(x => x.TfsServerConnectionDetails).Return(details).Repeat.Any();

            // Act 
            bool canExecute = _command.CanExecute(null);

            // Assert
            Assert.IsTrue(canExecute, "Should be able to execute command if using default credentials");
        }

        [TestMethod]
        public void CanExecuteDisabledIfServerUriMissing()
        {
            // Arrange
            TfsServerConnectionDetails details = new TfsServerConnectionDetails
            {
                UseDefaultCredentials = true,
                Username = string.Empty,
                Password = string.Empty,
                DomainName = string.Empty,
                TfsProjectPath = "$MyProject"
            };

            _mockViewModel.Expect(x => x.TfsServerConnectionDetails).Return(details).Repeat.Any();

            // Act 
            bool canExecute = _command.CanExecute(null);

            // Assert
            Assert.IsFalse(canExecute, "Should not be able to execute command if Server URI is missing");
        }

        [TestMethod]
        public void CanExecuteDisabledIfProjectPathIsMissing()
        {
            // Arrange
            TfsServerConnectionDetails details = new TfsServerConnectionDetails
            {
                UseDefaultCredentials = true,
                Username = string.Empty,
                Password = string.Empty,
                DomainName = string.Empty,
                TfsServerUrl = "http://localhost/tfs"
            };

            _mockViewModel.Expect(x => x.TfsServerConnectionDetails).Return(details).Repeat.Any();

            // Act 
            bool canExecute = _command.CanExecute(null);

            // Assert
            Assert.IsFalse(canExecute, "Should not be able to execute command if the project path is missing");
        }

        [TestMethod]
        public void CanExecuteDisabledIfDomainIsMissing()
        {
            // Arrange
            TfsServerConnectionDetails details = new TfsServerConnectionDetails
            {
                UseDefaultCredentials = false,
                Username = "bryan",
                Password = "MyPassword",
                TfsServerUrl = "http://localhost/tfs",
                TfsProjectPath = "$MyProject"
            };

            _mockViewModel.Expect(x => x.TfsServerConnectionDetails).Return(details).Repeat.Any();

            // Act 
            bool canExecute = _command.CanExecute(null);

            // Assert
            Assert.IsFalse(canExecute, "Should not be able to execute command if domain is not supplied");
        }

        [TestMethod]
        public void CanExecuteDisabledIfUsernameIsMissing()
        {
            // Arrange
            TfsServerConnectionDetails details = new TfsServerConnectionDetails
            {
                UseDefaultCredentials = false,
                Password = "MyPassword",
                DomainName = "Home",
                TfsServerUrl = "http://localhost/tfs",
                TfsProjectPath = "$MyProject"
            };

            _mockViewModel.Expect(x => x.TfsServerConnectionDetails).Return(details).Repeat.Any();

            // Act 
            bool canExecute = _command.CanExecute(null);

            // Assert
            Assert.IsFalse(canExecute, "Should not be able to execute command if the username is missing");
        }

        [TestMethod]
        public void CanExecuteDisabledIfPasswordIsMissing()
        {
            // Arrange
            TfsServerConnectionDetails details = new TfsServerConnectionDetails
            {
                UseDefaultCredentials = false,
                Username = "Bryan",
                DomainName = "Home",
                TfsServerUrl = "http://localhost/tfs",
                TfsProjectPath = "$MyProject"
            };

            _mockViewModel.Expect(x => x.TfsServerConnectionDetails).Return(details).Repeat.Any();

            // Act 
            bool canExecute = _command.CanExecute(null);

            // Assert
            Assert.IsFalse(canExecute, "Should not be able to execute command if the password is missing");
        }

        [TestMethod]
        public void CanExecuteEnabledIfAllCustomCredentialsArePresent()
        {
            // Arrange
            TfsServerConnectionDetails details = new TfsServerConnectionDetails
            {
                UseDefaultCredentials = false,
                Username = "Bryan",
                Password = "MyPassword",
                DomainName = "Home",
                TfsServerUrl = "http://localhost/tfs",
                TfsProjectPath = "$MyProject"
            };

            _mockViewModel.Expect(x => x.TfsServerConnectionDetails).Return(details).Repeat.Any();

            // Act 
            bool canExecute = _command.CanExecute(null);

            // Assert
            Assert.IsTrue(canExecute, "Should be able to execute command if using default credentials");
        }

    }
}
